package com.zsh.wanandroid.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.zsh.wanandroid.impl.AdapterItemClickListener;
import com.zsh.wanandroid.ui.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsh
 * @date 2019/12/13.
 * 功能：adapter公共类
 */

public abstract class CommonAdapter<T> extends BaseAdapter {

    protected List<T> mList;
    private int layoutId;
    protected Context mContext;
    protected LayoutInflater mInflater;

    public CommonAdapter(Context context, List<T> list, int layoutId) {
        this.mContext = context;
        this.mList = list;
        this.layoutId = layoutId;
        this.mInflater = LayoutInflater.from(mContext);
    }

    public CommonAdapter(Context context, int layoutId) {
        this.mContext = context;
        this.layoutId = layoutId;
        this.mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<T> beans) {
        mList = new ArrayList<>();
        mList.addAll(beans);
        notifyDataSetChanged();
    }

    public void addData(List<T> beans) {
        if (beans == null) {
            return;
        }
        if (mList == null) {
            setData(beans);
        } else {
            mList.addAll(beans);
            notifyDataSetChanged();
        }
    }

    public void clearList() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (null == mList) {
            return 0;
        } else {
            return mList.size();
        }
    }


    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }


    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, layoutId, position, mInflater);
        //交给子类去重写该方法，实现自定义
        commonView(position, viewHolder, mList.get(position));
        return viewHolder.getmConvertView();
    }

    //子类实现该方法
    public abstract void commonView(int position, ViewHolder viewHolder, T obj);

    //监听接口
    public AdapterItemClickListener adapterItemClickListener; //定义监听接口对象

    public void setAdapterItemClickListener(AdapterItemClickListener adapterItemClickListener) {
        this.adapterItemClickListener = adapterItemClickListener;
    }

}
